package com.ruoyi.web.controller.xiaomi;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.bye.xiaomi_mall.domain.TdRotateImage;
import com.bye.xiaomi_mall.service.ITdRotateImageService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.bye.xiaomi_mall.domain.TdGoodsCategory;
import com.bye.xiaomi_mall.service.ITdGoodsCategoryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品类别Controller
 *
 * @author ruoyi
 * @date 2025-03-15
 */
@RestController
@RequestMapping("/xiaomi_mall/category")
public class TdGoodsCategoryController extends BaseController {
    @Autowired
    private ITdGoodsCategoryService tdGoodsCategoryService;

    /**
     * 查询商品类别列表
     */
    @PreAuthorize("@ss.hasPermi('xiaomi_mall:category:list')")
    @GetMapping("/list")
    public TableDataInfo list(TdGoodsCategory tdGoodsCategory) {
        startPage();
        List<TdGoodsCategory> list = tdGoodsCategoryService.selectTdGoodsCategoryList(tdGoodsCategory);
        return getDataTable(list);
    }

    /**
     * 导出商品类别列表
     */
    @PreAuthorize("@ss.hasPermi('xiaomi_mall:category:export')")
    @Log(title = "商品类别", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TdGoodsCategory tdGoodsCategory) {
        List<TdGoodsCategory> list = tdGoodsCategoryService.selectTdGoodsCategoryList(tdGoodsCategory);
        ExcelUtil<TdGoodsCategory> util = new ExcelUtil<TdGoodsCategory>(TdGoodsCategory.class);
        util.exportExcel(response, list, "商品类别数据");
    }

    /**
     * 获取商品类别详细信息
     */
    @PreAuthorize("@ss.hasPermi('xiaomi_mall:category:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tdGoodsCategoryService.selectTdGoodsCategoryById(id));
    }

    /**
     * 新增商品类别
     */
    @PreAuthorize("@ss.hasPermi('xiaomi_mall:category:add')")
    @Log(title = "商品类别", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TdGoodsCategory tdGoodsCategory) {
        return toAjax(tdGoodsCategoryService.insertTdGoodsCategory(tdGoodsCategory));
    }

    /**
     * 修改商品类别
     */
    @PreAuthorize("@ss.hasPermi('xiaomi_mall:category:edit')")
    @Log(title = "商品类别", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TdGoodsCategory tdGoodsCategory) {
        return toAjax(tdGoodsCategoryService.updateTdGoodsCategory(tdGoodsCategory));
    }

    /**
     * 删除商品类别
     */
    @PreAuthorize("@ss.hasPermi('xiaomi_mall:category:remove')")
    @Log(title = "商品类别", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        int i = tdGoodsCategoryService.deleteTdGoodsCategoryByIds(ids);
        if (i==-1){
            return AjaxResult.error("删除失败，该类别下还有商品");
        }
        if (i==-2){
            return AjaxResult.error("删除失败，该类别下还有优惠券");
        }

        return toAjax(i);
    }
}
